<?php
class CommonTag extends Tag{
    /**
     * 标签声明
     * @var array
     */
    public $Tag = array(
    	//导航标签
        'navigation' => array('block' => 1, 'level' => 4),
        //文章标签
        'goods' =>array('block' =>1, 'level' =>4),
        //调取评论的标签
        'comment' =>array('block' =>1, 'level' =>4),
        //调取分类的标签
        'channel'=>array('block' => 1, 'level' => 4),
    );

    /**
     * 测试标签
     * @param $attr 属性
     * @param $content 内容
     * @param $hd HdView模型引擎对象
     */
     
    /**
	 * 分类标签
	 */
    public function _channel($attr, $content, &$hd){
    	//取得的条数
		$rows = isset($attr['rows']) ? $attr['rows'] : 1000;
		//取得的cid
		$cid = isset($attr['cid']) ? $attr['cid'] : NULL;
		//获取分类的类型，son为子类，如果不传current 是当前分类
		$type = isset($attr['type']) ? $attr['type'] : 'current';

		//分类必须显示
		$where = 'is_show=1';
		//如果用户传递了cid
		if($cid){
			switch ($type) {
				//调取当前分类
				case 'current':
					$where .= ' and cid=' . $cid;
					break;
				
				//调取当前分类
				case 'son':
					$where .= ' and pid=' . $cid;
					break;
				default:
					# code...
					break;
			}

		}
		

		$str = <<<str
<?php
\$data = K('Cate')->where('{$where}')->limit('{$rows}')->all();
foreach(\$data as \$field):
//如果是静态的时候
if(\$field['is_listhtml']){
	\$field['url'] = '__STATIC__/' . C('index_tpl_style') . '/' . \$field['htmldir'] . '/index.html';
}else{
	\$field['url'] = U('List/index',array('cid'=>\$field['cid']));
}
?>
{$content}
<?php
endforeach;
?>
str;
		return $str;

    } 
   
    /**
	 * 导航标签
	 */
    public function _navigation($attr, $content, &$hd){
		//取得的条数
		$rows = isset($attr['rows'])? $attr['rows'] : 1000;
		//排序
		$order = isset($attr['order']) ? $attr['order'] : 'csort ASC';
		//组合页面所需要的字符串
		$str = <<<str
<?php
\$data = K('Cate')->where("pid=0")->order('$order')->limit('$rows')->all();
foreach(\$data as \$field):
\$field['url'] = U('List/index',array('cid'=>\$field['cid']));
?>
{$content}
<?php endforeach ?>
str;
		return $str;
    }	
	
	/**
	 * 文章标签
	 */
	public function _goods($attr, $content, &$hd){
	    //取得的条数
	    $rows = isset($attr['rows'])? $attr['rows']:1000;
		//商品设置不同的属性，热门，置顶
		$attrData = isset($attr['attr'])? $attr['attr']:'';
		//排序
		$order = isset($attr['order'])? $attr['order']:'sendtime DESC';
		
		//组合where条件，首先必须有的条件，不在回收站
		$where = "is_recycle=0";
		//如果用户有传递属性（热门，置顶）的时候
		if($attrData){
			$where .= " and attr like '%{$attrData}%'";
		}
		
		//分类cid
		$cid = Q('get.cid',0,'intval');
		//如果在列表页，因为在列表页才有cid
		if($cid){
			//要获得所有的子集cid,因为点击顶级分类的时候，所有的子集的文章都得出来
			$cids = K('Cate')->sonCids($cid);
			$where .= ' and category_cid in(' . implode(',',$cids) . ')';
		}
		
		//如果是有tid的时候，证明是点击了标签云
		$tid = Q('get.tid',0,'intval');
		//如果有tid的时候，我们要根据标签来查询文章
		if($tid){
			//查询中间表，获得标签所对应的文章
			$aids = K('ArticleTag')->where("tag_tid={$tid}")->getField('article_aid',true);
			//如果标签对应的有文章
			if($aids){
				$where .= ' and aid in(' . implode(',', $aids) . ')';
			}else{
				//标签没有的对应的文章，那就不能查出来
				//false就是为找不到文章
				$where .= ' and false';
			}
		}
		
		
	
		//分页处理，判断有没有pageRows这个属性
		//每页显示多少条
		$pageRows = isset($attr['pageRows'])? $attr['pageRows']:0;
		//显示多少个页码(默认值之所以为5，是为了防止用户忘记写页码)
		$pageNum = isset($attr['$pageNum'])? $attr['$pageNum']:5;
		
		
		$str = <<<str
<?php
	//如果要使用分页
	if($pageRows){
		//获得总数
		\$total = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->count();
		\$pageObj = new Page(\$total,{$pageRows},{$pageNum});
		//获得数据
		\$data = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->limit(\$pageObj->limit())->order('{$order}')->all();
		//显示分页
		\$page = \$pageObj->show();
	}else{
		//不使用分页
		\$data = M()->join('__article__ a JOIN __category__ c ON a.category_cid=c.cid')->where("{$where}")->limit('{$rows}')->order('{$order}')->all();
	}
	
	foreach (\$data as \$field):
	//把某些字段处理以后再压入或者添加某些字段
	\$field['thumb'] = '__ROOT__' . '/' . \$field['thumb'];
	\$field['url'] = U('Details/index',array('aid'=>\$field['aid']));
	\$field['total'] = K('Comment')->where("article_aid=" . \$field['aid'])->count();
	\$field['curl'] = U('List/index',array('cid'=>\$field['cid']));
?>	
{$content}
<?php
	endforeach;
?>
str;
	return $str;
	}

	
	//评论
	public function _comment($attr, $content, &$hd){
			//取得的条数
		$rows = isset($attr['rows'])? $attr['rows'] : 1000;
		//排序
		$order = isset($attr['order']) ? $attr['order'] : 'addtime DESC';
		//获得文章的aid
		$gid = Q('get.gid',0,'intval');
		//组合页面所需要的字符串
		$str = <<<str
<?php
\$data = K('Comment')->where("gid={$gid}")->order('$order')->limit('$rows')->all();
foreach(\$data as \$field):
?>
{$content}
<?php endforeach ?>
str;
		return $str;   
	}
	
	
	
}